﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WCS.Infrastructure.Attr;
using WCS.Infrastructure.Dto.Request.Coupon;
using WCS.Infrastructure.Dto.Request.Lottery;
using WCS.Infrastructure.EFCore;
using WCS.Service.Interface;

namespace WCS.Api.Admin.Controllers
{

    /// <summary>
    /// 优惠券模块
    /// </summary>
    public class CouponController : BaseController
    {
        private readonly IConfiguration _configuration;
        private readonly ICouponService _couponService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="couponService"></param>
        /// <param name="userService"></param>
        public CouponController(IConfiguration configuration, ICouponService couponService, IUserService userService) : base(userService)
        {
            _configuration = configuration;
            _couponService = couponService;
        }

        /// <summary>
        /// 新增优惠券模板
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增优惠券模板", Group = "优惠券模块")]
        public async Task<IActionResult> AddCouponTemplate(CouponTemplateCreateRequestDto dto)
        {
            var result = await _couponService.AddCouponTemplateAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取单个优惠券模板详情
        /// </summary>
        /// <param name="id">优惠券模板ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCouponTemplateDetail(string id)
        {
            var result = await _couponService.GetCouponTemplateDetailAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 修改优惠券模板
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "修改优惠券模板", Group = "优惠券模块")]
        public async Task<IActionResult> UpdateCouponTemplate(CouponTemplateCreateRequestDto dto)
        {
            var result = await _couponService.UpdateCouponTemplateAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除优惠券模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionLog(Action = "删除优惠券模板", Group = "优惠券模块")]
        public async Task<IActionResult> DeleteCouponTemplateList(string id)
        {
            var result = await _couponService.DeleteCouponTemplateListAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 优惠券模板增加库存
        /// </summary>
        /// <param name="dto">TemplateId、IncreaseAmount</param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "优惠券模板增加库存", Group = "优惠券模块")]
        public async Task<IActionResult> IncreaseCouponTemplateInventory(IncreaseCouponTemplateInventoryRequestDto dto)
        {
            var result = await _couponService.IncreaseCouponTemplateInventoryAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取当前门店/商户下优惠券模板列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页条数</param>
        /// <param name="templateCode">查询条件【根据优惠券模板编号】</param>
        /// <param name="storeId">门店ID</param>
        /// <param name="merchantId">商户ID</param>
        /// <param name="codeId">收款码ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCouponTemplateList(int page = 1, int limit = 10, string? templateCode = "", string? name = "", string? storeId = "", string? merchantId = "", string? codeId = "")
        {
            var result = await _couponService.GetCouponTemplateListAsync(new CouponTemplateQueryRequestDto
            {
                Page = page,
                Limit = limit,
                TemplateCode = templateCode,
                Name = name,
                StoreId = storeId,
                MerchantId = merchantId,
                CodeId = codeId,
            });
            return Ok(result);
        }

        /// <summary>
        /// 新增优惠券包【Name,Description,MerchantId,StartTime,EndTime,Status,ids(需要打包的优惠券模板Id集合)】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增优惠券包", Group = "优惠券模块")]
        public async Task<IActionResult> AddCouponPackage(CouponPackageRequestDto dto)
        {
            var result = await _couponService.AddCouponPackageAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除优惠券包【Id】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "删除优惠券包", Group = "优惠券模块")]
        public async Task<IActionResult> DeleteCouponPackage(CouponPackageRequestDto dto)
        {
            var result = await _couponService.DeleteCouponPackageAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取单个优惠券包详情【Id】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetCouponPackage(CouponPackageRequestDto dto)
        {
            var result = await _couponService.GetCouponPackageAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取优惠券包列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页条数</param>
        /// <param name="key">查询条件【根据优惠券包编号】</param>
        /// <param name="merchantId">商户ID</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetCouponPackageList(int page = 1, int limit = 10, string? key = "", string? merchantId = "")
        {
            var result = await _couponService.GetCouponPackageListAsync(new CouponPackageRequestDto
            {
                Page = page,
                Limit = limit,
                Key = key,
                MerchantId = merchantId
            });
            return Ok(result);
        }

        /// <summary>
        /// 编辑优惠券包【Id,Name、Description、QrcodeUrl、StartTime、EndTime、Status、EnableStatus、ExtraProperties,ids】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "编辑优惠券包", Group = "优惠券模块")]
        public async Task<IActionResult> UpdateCouponPackage(CouponPackageRequestDto dto)
        {
            var result = await _couponService.UpdateCouponPackageAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 领取优惠券包【PackageId，ReceiveChannel】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "领取优惠券包", Group = "优惠券模块")]
        public async Task<IActionResult> ReceiveCouponPackage(CouponPackageReceiveRecordRequestDto dto)
        {
            var result = await _couponService.ReceiveCouponPackageAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 计算优惠券实例(计算优惠金额，实付金额，服务费)
        /// </summary>
        /// <param name="couponInstanceId">优惠券实例ID</param>
        /// <param name="orderAmount">订单金额</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> CalculateCouponInstance(string? couponInstanceId = "", long? orderAmount = 0)
        {
            var result = await _couponService.CalculateCouponInstanceAsync(new CalculateCouponInstanceRequestDto
            {
                CouponInstanceId = couponInstanceId,
                OrderAmount = (long)orderAmount
            });
            return Ok(result);
        }

        /// <summary>
        /// 平台随机推送优惠券(五张)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> PlatformRandomlyPusheCoupon(int page = 1, int limit = 5, string? merchantId = "", string? storeId = "")
        {
            var rsult = await _couponService.PlatformRandomlyPusheCouponAsync(new CouponTemplateQueryRequestDto
            {
                Page = page,
                Limit = limit,
                MerchantId = merchantId,
                StoreId = storeId,
            });
            return Ok(rsult);
        }

        /// <summary>
        /// 用户领取优惠券
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "用户领取优惠券", Group = "优惠券模块")]
        public async Task<IActionResult> UserGetCoupon(CouponInstanceCreateRequestDto dto)
        {
            var result = await _couponService.UserGetCouponAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 礼品券核销
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "用户礼品券核销", Group = "优惠券模块")]
        public async Task<IActionResult> RedeemGiftCoupon(GiftCouponRedeemRequestDto dto)
        {
            var result = await _couponService.RedeemGiftCouponAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 撤销核销（支付失败/取消/超时等场景调用）
        /// </summary>
        /// <param name="couponInstanceId"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionLog(Action = "撤销核销", Group = "优惠券模块")]
        public async Task<IActionResult> CancelCouponConsumption(string couponInstanceId, string orderId)
        {
            var result = await _couponService.CancelCouponConsumptionAsync(couponInstanceId, orderId);
            return Ok(result);
        }


        /// <summary>
        /// 获取用户优惠券实例列表
        /// </summary>
        /// <param name="status">状态：1=未使用，2=已使用，3=已过期</param>
        /// <param name="merchantId">可用商户ID</param>
        /// <param name="storeId">可用门店ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserCouponInstanceList(int page = 1, int limit = 10, int status = 1, string? merchantId = "", string? storeId = "")
        {
            var result = await _couponService.GetUserCouponInstanceListAsync(new CouponInstanceQueryRequestDto
            {
                Page = page,
                Limit = limit,
                MerchantId = merchantId,
                StoreId = storeId,
                Status = status,
            });
            return Ok(result);
        }


        /// <summary>
        /// 获取优惠券核销记录列表(当前用户)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCouponConsumptionRecordList()
        {
            var result = await _couponService.GetCouponConsumptionRecordListAsync();
            return Ok(result);
        }

        /// <summary>
        /// 过期优惠券实例处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExpireCouponInstances()
        { 
            var result = await _couponService.ExpireCouponInstancesAsync(null, null);
            return Ok(result);
        }

        ///// <summary>
        ///// 获取单个优惠券核销记录详情
        ///// </summary>
        ///// <param name="Id">优惠券核销记录ID</param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<IActionResult> GetCouponConsumptionRecordAsync(string? Id = "")
        //{
        //    var result = await _couponService.GetCouponConsumptionRecordAsync(new CouponConsumptionRecordRequestDto
        //    {
        //        Id = Id
        //    });
        //    return Ok(result);
        //}

        /// <summary>
        /// 新增抽奖活动
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增抽奖活动", Group = "抽奖活动模块")]
        public async Task<IActionResult> AddLotteryActivity(LotteryActivityCreateRequestDto dto)
        {
            var result = await _couponService.AddLotteryActivityAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取抽奖活动列表
        /// </summary>
        /// <param name="status">活动状态：1=草稿，2=上架，3=下架（为null查询所有状态）</param>
        /// <param name="activityCode">活动编号</param>
        /// <param name="name">活动名称</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetLotteryActivityList(int? status, string? activityCode, string? name, int page = 1, int limit = 10)
        {
            var result = await _couponService.GetLotteryActivityListAsync(new LotteryActivityQueryRequestDto
            {
                Page = page,
                Limit = limit,
                Status = status,
                ActivityCode = activityCode,
                Name = name,
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个抽奖活动详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetLotteryActivityDetail(string id)
        {
            var result = await _couponService.GetLotteryActivityDetailAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 编辑单个抽奖活动基本信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "修改抽奖活动", Group = "抽奖活动模块")]
        public async Task<IActionResult> UpdateLotteryActivity(LotteryActivityCreateRequestDto dto)
        {
            var result = await _couponService.UpdateLotteryActivityAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 编辑抽奖活动状态
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "修改抽奖活动状态", Group = "抽奖活动模块")]
        public async Task<IActionResult> UpdateLotteryActivityStatus(LotteryActivityUpdateRequestDto dto)
        {
            var result = await _couponService.UpdateLotteryActivityStatusAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除抽奖活动
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "删除抽奖活动", Group = "抽奖活动模块")]
        public async Task<IActionResult> DeleteLotteryActivity(string id)
        {
            var result = await _couponService.DeleteLotteryActivityAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 新增抽奖奖品（批量）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增抽奖奖品", Group = "抽奖活动模块")]
        public async Task<IActionResult> AddLotteryPrizes(LotteryPrizeCreateRequestDto dto)
        {
            var result = await _couponService.AddLotteryPrizesAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 编辑抽奖奖品（批量）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "编辑抽奖奖品", Group = "抽奖活动模块")]
        public async Task<IActionResult> UpdateLotteryPrizes(LotteryPrizeCreateRequestDto dto)
        {
            var result = await _couponService.UpdateLotteryPrizesAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 点击抽奖
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "点击抽奖", Group = "抽奖活动模块")]
        public async Task<IActionResult> LotteryDraw(LotteryDrawRequestDto dto)
        {
            var result = await _couponService.LotteryDrawAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取抽奖与发券记录列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="activityId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetLotteryClaimRecordList(int page = 1, int limit = 10, string activityId = "", string? userId = "")
        {
            var result = await _couponService.GetLotteryClaimRecordListAsync(new LotteryClaimRecordQueryRequestDto
            {
                Page = page,
                Limit = limit,
                ActivityId = activityId,
                UserId = userId,
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取用户奖品列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="activityId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserLotteryPrizesList(int page = 1, int limit = 10, string activityId = "", string? userId = "")
        {
            var result = await _couponService.GetUserLotteryPrizesListAsync(new LotteryClaimRecordQueryRequestDto
            {
                Page = page,
                Limit = limit,
                ActivityId = activityId,
                UserId = userId,
            });
            return Ok(result);
        }

        /// <summary>
        /// 激活优惠券实例
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "激活优惠券实例", Group = "优惠券模块")]
        public async Task<IActionResult> ActivateCouponInstance(LotteryPrizeActivateRequestDto dto)
        { 
            var result = await _couponService.ActivateCouponInstanceAsync(dto);
            return Ok(result);
        }
    } 
}
